/**
 ******************************************************************************
 * @file    ringbuf.c
 * @author  chengbb
 * @version V1.0
 * @date    2017-08-31
 * @brief   数组构成环形缓冲
 ******************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * <h2><center>&copy; COPYRIGHT 2017 MindX</center></h2>
 ******************************************************************************
 */
/** @addtogroup CONSOLE
* @{
*/

#include "ringbuf.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

/**
 ******************************************************************************
 * @brief  读取缓冲区数据长度
 * @param  buf_t    缓冲区指针.
 * @return 数据长度.
 ******************************************************************************/
int ringbuf_datalen(RingBuf_T* buf_t)
{
	return ((buf_t->iwrite >= buf_t->iread) ? (buf_t->iwrite - buf_t->iread): (buf_t->buf_size - buf_t->iread + buf_t->iwrite));
}

/**
 ******************************************************************************
 * @brief  初始化环形缓冲区
 * @param  buf_t    缓冲区指针.
 * @param  buf      指向buffer的指针
 * @param  buf_size buffer的size
 * @return None.
 ******************************************************************************/
void ringbuf_init(RingBuf_T *buf_t, uint8_t *buf, uint16_t buf_size)
{
	memset(buf_t, 0, sizeof(RingBuf_T));
	buf_t->buf = buf;
	buf_t->buf_size = buf_size;
}

/**
 ******************************************************************************
 * @brief  地址转换
 * @param  buf_t 缓冲区结构指针.
 * @param  addr 当前地址
 * @return 下一步的地址.
 ******************************************************************************/
static int next_data_handle(RingBuf_T *buf_t, int addr)
{
	if((addr + 1) == buf_t->buf_size)
	{
		return 0;
	}
	else
	{
		return (addr + 1);
	}
}

/**
 ******************************************************************************
 * @brief  读操作
 * @param  buf_t 缓冲区结构指针.
 * @return 结果
 * @retval 0未满
 * @retval 1已满
 ******************************************************************************/
int ringbuf_isfull(RingBuf_T* buf_t)
{
	if((buf_t->iwrite) == (buf_t->iread - 1))
	{
		return 0;
	}
	else
	{
		return 1;
	}	
}


/**
 ******************************************************************************
 * @brief  写操作
 * @param  buf_t 缓冲区结构指针.
 * @param  ch    一个字节数据
 * @return 当前未读数据长度, 若缓冲区满返回-1
 ******************************************************************************/
int ringbuf_write(RingBuf_T *buf_t, uint8_t ch)
{
	if((buf_t->buf != NULL) && (ringbuf_isfull(buf_t)))
	{
		buf_t->buf[buf_t->iwrite] = ch;                             
		buf_t->iwrite = next_data_handle(buf_t, buf_t->iwrite);   
		return 1;
	}
	else
	{
		return -1;
	}
}

/**
 ******************************************************************************
 * @brief  读操作
 * @param  buf_t 缓冲区结构指针.
 * @return 一个字节数据, 无数据返回-1
 ******************************************************************************/
int ringbuf_hasdata(RingBuf_T* buf_t)
{
	if(buf_t->iread != buf_t->iwrite)
	{
		return 1;
	}
	else
	{
		return 0;
	}
//    return (buf_t->iread != buf_t->iwrite);
}

/**
 ******************************************************************************
 * @brief  读操作
 * @param  buf_t 缓冲区结构指针.
 * @return 一个字节数据, 无数据返回-1
 ******************************************************************************/
int ringbuf_read(RingBuf_T* buf_t)
{
    uint8_t ch;

    if(ringbuf_hasdata(buf_t))
    {
        ch = buf_t->buf[buf_t->iread];
        buf_t->iread = next_data_handle(buf_t, buf_t->iread);

        return ch;
    }
    else
    {
        return -1;
    }
}
/**
  * @}
  */
/******************* (C) COPYRIGHT 2017 MINDX ******END OF FILE******************/


